package server

import (
	"gitee.com/mootok/mootok/src/web/api"
	"gitee.com/mootok/mootok/src/web/handlers"
	middleware_local "gitee.com/mootok/mootok/src/web/middleware"
	middleware_oapi "github.com/deepmap/oapi-codegen/pkg/gin-middleware"
	"github.com/getkin/kin-openapi/openapi3"
	"github.com/getkin/kin-openapi/openapi3filter"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"time"
)

func SetupRouters() *gin.Engine {
	router := gin.Default()
	openapi3.SchemaErrorDetailsDisabled = true
	swagger, err := api.GetSwagger()

	if err != nil {
		log.Fatalf("GetSwagger: %s\n", err)
	}

	router.Use(middleware_oapi.OapiRequestValidatorWithOptions(swagger,
		&middleware_oapi.Options{
			ErrorHandler: middleware_local.AuthenticationErrorHandlerMiddleWare,
			Options: openapi3filter.Options{
				AuthenticationFunc: middleware_local.AuthenticationFuncMiddleWare,
			},
		}))
	router.Use(middleware_local.RateLimiterMiddleWare(time.Second, 1000, 1000))

	var apiImpl handlers.ApiImpl
	api.RegisterHandlers(router, &apiImpl)
	return router
}
